package android.demo.engine.tasks;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.demo.engine.ProcessStateManager;
import android.demo.engine.ServiceCompletionListener;
import android.demo.engine.visitor.TaskVisitor;

public abstract class ServiceTask extends Task {

	private List<ServiceCompletionListener> listeners;
	protected Intent intent;
	
	public ServiceTask(int id) {
		super(id);
		listeners = new ArrayList<ServiceCompletionListener>();
	}
	public void addServiceCompletionListener(ServiceCompletionListener listener){
		listeners.add(listener);
	}
	
	public void removeListener(ServiceCompletionListener listener){
		listeners.remove(listener);
	}
	protected void notifyCompletion(){
		for(ServiceCompletionListener l : listeners)
			l.onServiceCompletion(this);
	}

	@Override
	public void accept(TaskVisitor visitor) {
			visitor.visit(this);
	}

	public Intent getIntent(Context process, ProcessStateManager processState) {
		return intent;
	}
	public void finish(){
		super.finish();
		this.intent=null;
		notifyCompletion();
	}

	public void init(){
		super.init();
		this.intent=null;
	};
}
